package com.sugon.controller.tool;

import com.sugon.entity.ToolsTypeEntity;
import com.sugon.service.ToolsTypeService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工装类别Controller
 *
 * @author sugon
 * @date 2019-08-01 08:59:25
 */
@Controller
@RequestMapping("toolstype")
public class ToolsTypeController {
    @Autowired
    private ToolsTypeService toolsTypeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("toolstype:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {

        //查询列表数据
        Query query = new Query(params);

        List<ToolsTypeEntity> toolsTypeList = toolsTypeService.queryList(query);
        int total = toolsTypeService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsTypeList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("toolstype:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ToolsTypeEntity toolsType = toolsTypeService.queryObject(id);

        return R.ok().put("toolsType", toolsType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("toolstype:save")
    @ResponseBody
    public R save(@RequestBody ToolsTypeEntity toolsType) {
        // 类别代码不允许重复
        ToolsTypeEntity toolsTypeInDB = toolsTypeService.queryObjectByTypeCode(toolsType.getTypeCode());
        if (toolsTypeInDB != null) {
            return R.error("类别编码不允许重复！");
        }
        return toolsTypeService.save(toolsType) > 0 ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("toolstype:update")
    @ResponseBody
    public R update(@RequestBody ToolsTypeEntity toolsType) {
        // 校验工装类别是否存在
        ToolsTypeEntity toolsTypeInDB = toolsTypeService.queryObject(toolsType.getId());
        if (toolsTypeInDB == null) {
            return R.error("工装类别不存在！");
        }

        // 类别代码不允许重复
        String typeCode = toolsType.getTypeCode();
        if (!toolsTypeInDB.getTypeCode().equals(typeCode) && toolsTypeService.queryObjectByTypeCode(typeCode) != null) {
            return R.error("类别编码不允许重复！");
        }
        return toolsTypeService.update(toolsType) > 0 ? R.ok() : R.error();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("toolstype:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        toolsTypeService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsTypeEntity> list = toolsTypeService.queryList(params);

        return R.ok().put("list", list);
    }
}
